﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LoghogInterface.ViewModels;
using LoghogInterface.Modules;
using LoghogInterface.Properties;

namespace LoghogInterface
{
    /// <summary>
    /// Логика взаимодействия для AddEvent.xaml
    /// </summary>
    public partial class AddEvent : Window
    {
        private JournalSQLHelper _journalSqlHelper;
        private int _idJournal;
        private Settings _settings;

        public AddEvent(int idJournal)
        {
            _idJournal = idJournal;
            _journalSqlHelper = new JournalSQLHelper();
            _settings = new Settings();

            InitializeComponent();
            this.CreateControls();
        }

        private void CreateControls()
        {

            var fields = _journalSqlHelper.GetFieldsJournal(_idJournal);

            foreach (var field in fields)
            {
                if (field.Type.Name != _settings.TypeImage)
                {
                    var label = new Label();
                    label.Content = field.NameRus;

                    var box = new TextBox();
                    this.RegisterName(field.Name, box);
                    Panel.Children.Add(label);
                    Panel.Children.Add(box);
                }
                else
                {
                    var label = new Label();
                    label.Content = field.NameRus;

                    var button = new Button();
                    button.Content = Properties.Resources.Browser;
                    button.Width = 50;
                    button.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    button.Click += new RoutedEventHandler(this.Browse);

                    var label2 = new Label();

                    this.RegisterName(_settings.TypeImage, label2);

                    Panel.Children.Add(label);
                    Panel.Children.Add(label2);
                    Panel.Children.Add(button);
                }
            }
            var checkBox = new CheckBox();
            checkBox.Content = "Черновик";
            this.RegisterName("draft", checkBox);
            Panel.Children.Add(checkBox);

            var panelButton = new StackPanel();
            panelButton.Orientation = Orientation.Horizontal;
            panelButton.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;

            var buttonAdd = new Button();
            buttonAdd.Content = Properties.Resources.Add;
            buttonAdd.Click += new RoutedEventHandler(this.EventAdd);

            var buttonCancel = new Button();
            buttonCancel.Content = Properties.Resources.Cancel;
            buttonCancel.Click += new RoutedEventHandler(this.CloseThisWindow);

            panelButton.Children.Add(buttonAdd);
            panelButton.Children.Add(buttonCancel);

            Panel.Children.Add(panelButton);
        }

        private void Browse(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
            var showDialog = fileDialog.ShowDialog();
            if (showDialog == System.Windows.Forms.DialogResult.OK)
            {
                string filename = fileDialog.FileName;
                var label = (Label)this.FindName(_settings.TypeImage);
                if (label != null)
                {
                    label.Content = filename;
                }
            }
        }

        private void EventAdd(object sender, RoutedEventArgs e)
        {
            var fields = _journalSqlHelper.GetFieldsJournal(_idJournal);
            var datetime = DateTime.Now;
            var draft = (CheckBox)this.FindName("draft");
            bool draftState = (bool)draft.IsChecked;

            foreach (var field in fields)
            {
                if (field.Type.Name != _settings.TypeImage)
                {
                    var box = (TextBox)this.FindName(field.Name);
                    if (box != null)
                    {
                        _journalSqlHelper.InsertEvent(box.Text, datetime, draftState, field.ID);
                    }
                }
                else
                {
                    _journalSqlHelper.InsertEvent("картинка", datetime, draftState, field.ID);
                }
            }
            _journalSqlHelper.ApplyChanges();
            this.Close();
        }

        private void CloseThisWindow(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
